package edu.fim.presentation.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Observable;

import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;

import edu.fim.presentation.controller.ConsumerController;
import edu.fim.presentation.controller.FoodController;
import edu.fim.presentation.model.bean.Food;


public class AddFoodForm extends AbstractForm {

	private static final long serialVersionUID = 6666881458741167397L;
	
	private AbstractForm parentForm;
	private ConsumerController consumerController;
	private FoodController foodController;
	private JList<Food> foodsToBeIncluded;

	public AddFoodForm(
			String title,
			AbstractForm parentForm,
			ConsumerController consumerController,
			FoodController foodController) {
		super(title);
		this.parentForm = parentForm;
		this.consumerController = consumerController;
		this.foodController = foodController;
		initialize();
		update(null, null);
	}

	public void update(Observable arg0, Object arg1) {
		DefaultListModel<Food> foodListModel = new DefaultListModel<>();
		try {
			for (Food food : foodController.findAll()) {
				foodListModel.addElement(food);
			}
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		foodsToBeIncluded.setModel(foodListModel);
		pack();
	}

	protected void initializeDynamicComponents() {
		foodsToBeIncluded = new JList<>();
	}
	
	protected void buildLayout() {
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(foodsToBeIncluded);

		getContentPane().setLayout(
				new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));

		getContentPane().add(new JLabel("Select the foods to be added:"));
		getContentPane().add(scrollPane);
		getContentPane().add(createButtonContainer());
	}

	private Component createButtonContainer() {
		Container container = new Container();
		container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));

		container.add(createAddFoodComponent());
		container.add(createCancelComponent());

		return container;
	}

	private Component createAddFoodComponent() {
		AbstractButton button = new JButton("Add");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				addFoodComponentActionPerformed();
			}
		});
		return button;
	}
	
	private Component createCancelComponent() {
		AbstractButton button = new JButton("Cancel");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				cancelComponentActionPerformed();
			}
		});
		return button;
	}

	private void addFoodComponentActionPerformed() {
		for (Food food : foodsToBeIncluded.getSelectedValuesList()) {
			consumerController.getCurrentConsumer().getMenu().add(food);
		}
		parentForm.update(null, null);
		this.setVisible(false);
	}
	
	private void cancelComponentActionPerformed() {
		this.setVisible(false);
	}
}